import time

from hs_udata import set_token, fut_quote_minute, stock_quote_minutes, stock_quote_daily_list

set_token(token='ruXPpxAnX4-5DOF7SXgKOuI4y7WOtBPVXqE79Nj2q30dHluNv6Hd-ylE-JwynvBZ')
startDate = "2022-01-01"
endDate = "2022-01-01"
userList = []
stockList = []
futuresList = []
openEntrustInfo = False


def dayNum(day1, day2):
    time_array1 = time.strptime(day1, "%Y-%m-%d")
    timestamp_day1 = int(time.mktime(time_array1))
    time_array2 = time.strptime(day2, "%Y-%m-%d")
    timestamp_day2 = int(time.mktime(time_array2))
    result = (timestamp_day2 - timestamp_day1) // 60 // 60 // 24
    return result


def getMinDate(prod_code, nowDay):
    data = stock_quote_minutes(en_prod_code=str(prod_code),
                               begin_date=str(nowDay), end_date=str(nowDay))
    return data


def getFuturesData(code, start, end):
    print(startDate)
    print(start)
    data = fut_quote_minute(en_prod_code=code, begin_date=str(start), end_date=str(end))
    data.rename(columns={'date': 'date'}, inplace=True)
    data.rename(columns={'time': 'time'}, inplace=True)
    data.rename(columns={'open': 'open'}, inplace=True)
    data.rename(columns={'high': 'high'}, inplace=True)
    data.rename(columns={'low': 'low'}, inplace=True)
    data.rename(columns={'close': 'close'}, inplace=True)
    data.rename(columns={'turnover_value': 'value'}, inplace=True)
    data.rename(columns={'turnover_volume': 'volume'}, inplace=True)

    # data.drop(['amount', 'change_pct', 'change'], axis=1)
    return data


def getDayData(prod_code, start, end):
    data = stock_quote_daily_list(en_prod_code=str(prod_code), begin_date=str(start),
                                  end_date=str(end), adjust_way='1')

    data.sort_index(ascending=False, inplace=True)  # 设置排序
    data = data.reset_index(drop=True)
    return data


def nextDay():
    var1 = False
    var2 = False
    for stock in stockList:  # 遍历选股列表
        var1 = stock.nextDay()  # 更新股票最新数据
    for futures in futuresList:
        var2 = futures.nextDay()

    return var1 or var2


def nextMin():
    var1 = False
    var2 = False
    for stock in stockList:
        var1 = stock.nextMin()  # 获取实时分钟数据
    for futures in futuresList:
        var2 = futures.nextMin()  # 遍历当日分钟完返回False

    return var1 or var2


def config(startDate_, endDate_):
    global startDate, endDate
    startDate = startDate_
    endDate = endDate_

